//
//  SDWebImageTool.m
//  jieshuibao
//
//  Created by 李小龙 on 2016/11/16.
//  Copyright © 2016年 com.jieshuibao1. All rights reserved.
//

#import "SDWebImageTool.h"
#import "UIImageView+UIActivityIndicatorForSDWebImage.h"

@implementation SDWebImageTool

/**
 cookid 重新登录后 配置
 */
+ (void)refreshCookieId:(NSString *)cookieId
{
    SDWebImageDownloader *manager = [SDWebImageManager sharedManager].imageDownloader;
    
    //    [manager setValue:kMainBaseHost forHTTPHeaderField:@"Host"];
    [manager setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [manager setValue:@"application/json;charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [manager setValue:cookieId forHTTPHeaderField:@"cookieId"];
}


/**
 类方法
 设置imageview 图片
 @param imageView 要设置图片的view
 @param path 图片上级路径
 @param name 图片路径
 @param placeholder 本地占位图名
 @param show 展示进度
 */
+ (void)imageView:(UIImageView *)imageView setImageWithUrlPath:(NSString *)path
          nameUrl:(NSString *)name placeHolder:(NSString *)placeholder showProgress:(BOOL)show
{
//    [SDWebImageTool refreshCookieId:[JSBUserModel shareUser].cookieID];
    
    UIImage *placeHolder = placeholder.length > 0 ? [UIImage imageNamed:placeholder] : nil;
    
    if (!imageView || path.length == 0 || name.length == 0) {
        imageView.image = placeHolder;
        return;
    }

    NSString *url = [NSString stringWithFormat:@"%@/%@/%@", kMainBaseUrl, path, name];
//    DeLog(@"url %@", url);
    SDWebImageOptions options = SDWebImageAllowInvalidSSLCertificates | SDWebImageProgressiveDownload;
    
    if (show) {
        [imageView setImageWithURL:[NSURL URLWithString:[url encodeURLString]] placeholderImage:placeHolder options:options usingActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        
    } else {
        [imageView sd_setImageWithURL:[NSURL URLWithString:[url encodeURLString]] placeholderImage:placeHolder options:options];
    }
}





+ (void)newImageView:(UIImageView *)imageView setImageWithUrlPath:(NSString *)path
             nameUrl:(NSString *)name placeHolder:(NSString *)placeholder showProgress:(BOOL)show completed:(SDCompletedBlock)completed
{

    UIImage *placeHolder = placeholder.length > 0 ? [UIImage imageNamed:placeholder] : nil;
    
    if (!imageView || path.length == 0 || name.length == 0) {
        imageView.image = placeHolder;
        return;
    }
    
    NSString *url = [NSString stringWithFormat:@"%@/%@/%@", kMainBaseUrl, path, name];
    SDWebImageOptions options = SDWebImageAllowInvalidSSLCertificates | SDWebImageProgressiveDownload;
    
    if (show) {
//        [imageView setImageWithURL:[NSURL URLWithString:[url encodeURLString]] placeholderImage:placeHolder options:options usingActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        
        [imageView sd_setImageWithURL:[NSURL URLWithString:[url encodeURLString]]  placeholderImage:placeHolder options:options progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            
//            if ((receivedSize == expectedSize) && (completed)) {
//                completed();
//            }
////
            
            
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            if (completed) {
                completed();
            }
        }];
        
    } else {
        [imageView sd_setImageWithURL:[NSURL URLWithString:[url encodeURLString]] placeholderImage:placeHolder options:options];
    }

}




+ (void)downloadSetImageWithUrlPath:(NSString *)path
                  nameUrl:(NSString *)name completed:(SDDownloadCompletedBlock)completed
{

    if (path.length == 0 || name.length == 0) {
        return;
    }
    
    NSString *url = [NSString stringWithFormat:@"%@/%@/%@", kMainBaseUrl, path, name];
//    SDWebImageOptions options = SDWebImageAllowInvalidSSLCertificates | SDWebImageProgressiveDownload;
    
    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    [manager downloadImageWithURL:[NSURL URLWithString:[url encodeURLString]] options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        
        
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
        
        if (completed) {
            completed(image, error, finished);
        }
        
    }];
}

+ (void)imageView:(UIImageView *)imageView imageName:(NSString *)imageName placeHolder:(NSString *)placeholder showProgress:(BOOL)show
{
    [SDWebImageTool imageView:imageView setImageWithUrlPath:kShowImage nameUrl:imageName placeHolder:placeholder showProgress:show];
}



/**
 清除缓存
 */
+ (void)didReciveMemoryWarning
{
    [[SDWebImageManager sharedManager].imageCache clearMemory];
    [[SDWebImageManager sharedManager] cancelAll];
}

@end
